package com.monitor.display_platform.business.thread.v2.impl;

import com.monitor.display_platform.business.thread.v2.TaskLifeCycle;
import com.monitor.display_platform.core.common.SpringApplicationContext;
import com.monitor.display_platform.core.exec.BaseThreadPoolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

public class TaskLifeCycleImpl<T> implements TaskLifeCycle<T> {
    private static final Logger log = LoggerFactory.getLogger(TaskLifeCycleImpl.class);

    @Override
    public void onStart(Thread thread) {
        System.out.println("i started");
    }

    @Override
    public void onRunning(Thread thread) {
        System.out.println("i am working");
    }

    @Override
    public void onFinish(Thread thread, T result) {
        System.out.println("i finished");
        System.out.println(result);
    }

    @Override
    public void onError(Thread thread, Exception e) {
        System.out.println("i went wrong");
        thread.interrupt();
        System.out.println(" ready to restart ");
        ExecutorService executor = SpringApplicationContext.getBean(ThreadPoolExecutor.class);
        executor.execute(thread);
        log.info("添加任务完成 {}",executor.toString());

        try {
            Thread.sleep(10000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        log.info("添加任务完成 succeddful {}",executor.toString());
    }
}
